package com.alumni_circle.controller.home;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.PictureBase64;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.Sponsor;
import com.alumni_circle.entity.home.VO.SponsorInsertVO;
import com.alumni_circle.entity.home.VO.SponsorViewVO;
import com.alumni_circle.entity.home.issue.UserSponsor;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.service.home.ISponsorService;
import com.alumni_circle.service.home.issue.IUserSponsorService;
import com.alumni_circle.utils.MsgPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

import static com.alumni_circle.core.constant.ResponseCode.PERMISSIONS;
/**
 * @author 龙小虬
 * @since 2020-07-06 22:58
 */
@Api(tags = "赞助管理")
@Controller
@RequestMapping("/sponsor")
public class SponsorController extends BaseController<Sponsor> {

    @Resource
    private ISponsorService sponsorService;

    @Resource
    private IUserSponsorService userSponsorService;

    @Resource
    MsgPermissions msgPermissions;

    /**
     * 增加信息
     * @param sponsorInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addSponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增赞助信息", notes = "增加赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="新增赞助信息",url="/sponsor/addSponsor")
    public ResponseModel addSponsor(@ApiParam(required = true, name = "sponsorInsertVO")
                                      @RequestBody SponsorInsertVO sponsorInsertVO) {
        String path = "";
        if(!StrUtil.hasEmpty(sponsorInsertVO.getPicture()) && sponsorInsertVO.getPicture().contains("{-temporary}")){
            path = sponsorInsertVO.getPicture();
            sponsorInsertVO.setPicture("");
        }
        Sponsor sponsor = ModelUtils.transform(sponsorInsertVO,Sponsor.class);
        User user = SecurityUtils.getUser();
        sponsor.setCreateBy(user.getId());
        sponsor.setCreateName(user.getName());
        sponsorService.save(sponsor);
        super.picturePath(1,sponsor.getId(),path);
        UserSponsor userSponsor = new UserSponsor();
        userSponsor.setSponsorId(sponsor.getId());
        userSponsor.setUserId(user.getId());
        userSponsorService.save(userSponsor);
        return new ResponseModel<>(sponsor);
    }

    /**
     * 更新
     * @param sponsorInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateSponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新赞助信息", notes = "更新赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新赞助信息",url="/sponsor/updateSponsor")
    public ResponseModel updateSponsor(@ApiParam(required = true, name = "sponsorInsertVO")
                                         @RequestBody SponsorInsertVO sponsorInsertVO) {
        if(!ObjectUtil.hasEmpty(sponsorInsertVO.getViews())){
            Sponsor sponsor = new Sponsor();
            sponsor.setViews(sponsorInsertVO.getViews());
            sponsor.setId(sponsorInsertVO.getId());
            sponsorService.updateById(sponsor);
        }else {
            if(msgPermissions.updateDelete(sponsorInsertVO.getId(), "sponsor")){
                return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
            }
            sponsorService.updateById(ModelUtils.transform(sponsorInsertVO, Sponsor.class));
        }
        return new ResponseModel<>("success");
    }

    /**
     * id搜
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectSponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询赞助信息", notes = "查询赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询赞助信息",url="/sponsor/selectSponsor")
    public ResponseModel selectSponsor(@ApiParam(required = true, name = "baseId")
                                         @RequestBody BaseId baseId) {

        Sponsor sponsor = sponsorService.getById(baseId.getId());
        return new ResponseModel<>(ModelUtils.transform(sponsor, SponsorViewVO.class));
    }

    /**
     * 搜索关键字信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchSponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询赞助信息", notes = "查询赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询赞助信息",url="/sponsor/searchSponsor")
    public ResponseListModel searchSponsor(@ApiParam(required = true, name = "pageBase")
                                       @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<SponsorViewVO>list = sponsorService.searchListSponsor(pageBase.getContent());
        for (SponsorViewVO sponsorViewVO : list) {
            //取消头像下载，使用微信提供的链接
//            sponsorViewVO.setIcon(PictureBase64.imageStr(sponsorViewVO.getIcon()));
            if (!StrUtil.hasEmpty(sponsorViewVO.getPicture())) {
                sponsorViewVO.setPicture(PictureBase64.thumbnailList(sponsorViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }

    /**
     * 删除信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteSponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除赞助信息", notes = "删除赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除赞助信息",url="/sponsor/deleteSponsor")
    public ResponseModel deleteSponsor(@ApiParam(required = true, name = "baseId")
                                         @RequestBody BaseId baseId) {
        if(msgPermissions.updateDelete(baseId.getId(), "gossip")){
            return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
        }
        sponsorService.removeById(baseId.getId());
        QueryWrapper<UserSponsor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sponsor_id",baseId.getId());
        userSponsorService.remove(queryWrapper);
        super.picturePathDelete(baseId.getId());
        return new ResponseModel();
    }



    /**
     * 搜索所有信息并排序
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectListSponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "赞助信息", notes = "赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="赞助信息",url="/sponsor/selectListSponsor")
    public ResponseListModel selectListSponsor(@ApiParam(required = true, name = "pageBase")
                                               @RequestBody PageBase pageBase) {

        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<SponsorViewVO>list = sponsorService.selectListSponsor(pageBase.getContent());
        PageInfo<SponsorViewVO>pageInfo = new PageInfo<>(list);
        for (SponsorViewVO sponsorViewVO : list) {
            //取消头像下载，使用微信提供的链接
//            sponsorViewVO.setIcon(PictureBase64.imageStr(sponsorViewVO.getIcon()));
            if (!StrUtil.hasEmpty(sponsorViewVO.getPicture())) {
                sponsorViewVO.setPicture(PictureBase64.thumbnailList(sponsorViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    /**
     *
     * 用户所有发布的信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectMyListSponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "赞助信息", notes = "赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="赞助信息",url="/sponsor/selectMyListSponsor")
    public ResponseListModel selectMyListSponsor( @ApiParam(required = true, name = "pageBase")
                                                  @RequestBody PageBase pageBase) {

        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<SponsorViewVO>list = sponsorService.selectMyListSponsor(SecurityUtils.getId());
        for (SponsorViewVO sponsorViewVO : list) {
            //取消头像下载，使用微信提供的链接
//            sponsorViewVO.setIcon(PictureBase64.imageStr(sponsorViewVO.getIcon()));
            if (!StrUtil.hasEmpty(sponsorViewVO.getPicture())) {
                sponsorViewVO.setPicture(PictureBase64.thumbnailList(sponsorViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }


    /**
     * 用户本人的信息，关键字搜索
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchMySponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索赞助信息", notes = "搜索赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索赞助信息",url="/sponsor/searchMySponsor")
    public ResponseListModel searchMySponsor(@ApiParam(required = true, name = "pageBase")
                                             @RequestBody PageBase pageBase) {
        List<SponsorViewVO>list = sponsorService.searchMySponsor(SecurityUtils.getId(),pageBase.getContent());
        for (SponsorViewVO sponsorViewVO : list) {
            //取消头像下载，使用微信提供的链接
//            sponsorViewVO.setIcon(PictureBase64.imageStr(sponsorViewVO.getIcon()));
            if (!StrUtil.hasEmpty(sponsorViewVO.getPicture())) {
                sponsorViewVO.setPicture(PictureBase64.thumbnailList(sponsorViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }




    @ResponseBody
    @RequestMapping(value = "/getAllSponsor", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询符合条件赞助信息", notes = "查询符合条件赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询符合条件赞助信息",url="/sponsor/getAllSponsor")
    public ResponseListModel getAllSponsor(@ApiParam(required = true, name = "baseId")
                                           @RequestBody BaseId baseId) {
        System.out.println(baseId.getId());
        return new ResponseListModel<>(sponsorService.selectListSponsor(baseId.getId()));
    }

    @ResponseBody
    @RequestMapping(value = "/selectSponsorWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索赞助信息", notes = "搜索赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索赞助信息",url="/sponsor/selectSponsorWrapper")
    public ResponseListModel<Sponsor> selectClubActivityWrapper(@ApiParam(required = true, name = "sponsor")
                                                                @RequestBody Sponsor sponsor) {
        return new ResponseListModel<>(sponsorService.list(new QueryWrapper<Sponsor>().and(warpper -> warpper.like("unit",sponsor.getUnit()).or().like("content",sponsor.getContent()))));


    }

    @ResponseBody
    @RequestMapping(value = "/selectMySponsorWrapper", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索赞助信息", notes = "搜索赞助信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索赞助信息",url="/sponsor/selectMySponsorWrapper")
    public ResponseListModel<Sponsor> selectMySponsorWrapper(@ApiParam(required = true, name = "sponsor")
                                                             @RequestBody Sponsor sponsor) {
        return new ResponseListModel<>(sponsorService.list(new QueryWrapper<Sponsor>().and(warpper -> warpper.like("unit",sponsor.getUnit()))));

    }

}
